# include <stdio.h>
# include <string.h>
# include "Header.h"
# include "DirectoryOps.h"

int moveDirectory(char *srcPath, char *destPath, struct VFS *header) {
	int i, j, l, ret = 0;
	char temp[100];
	for(i = 0; i < MAX_FD_SIZE; i++) {
		if(header -> freeList[i]) {
			j = compareLength(destPath, header -> fd[i].filePath);
			if(j == stringLength(destPath) && header -> fd[i].size != 0) 
				return 7;
		}
	}
	
	j = compareLength(srcPath, destPath);
	if(j == stringLength(srcPath))
		return 6;
	for(i = 0; i < MAX_FD_SIZE; i++) {
		if(header -> freeList[i]) {
			j = compareLength(srcPath, header -> fd[i].filePath);
			if(j == stringLength(srcPath) || (j == (stringLength(srcPath) - 1) && srcPath[stringLength(srcPath) - 1] == '/') ) {
				if (header -> fd[i].size == 0) {
					for(l = 0; l < 100; l++) {
						temp[l] = '\0';	
					}
					copyString(temp, getLastDir(srcPath));
					validateDirPath(destPath, temp);			
					strconcat(destPath, temp);
					//strcat(destPath, (header -> fd[i].filePath) + j);
					validateDirPath(temp, (header -> fd[i].filePath) + j); 
					copyString(header -> fd[i].filePath, strconcat(temp, (header -> fd[i].filePath) + j));
					ret = 0;
				}
				else
					ret = 4;
			}
		}
	}
	return ret;
}
char *getLastDir(char *dir) {
	int j = stringLength(dir);
	if ( *(dir + j - 1) != '/') {
		while( *(dir + j - 1) != '/' ) {
		    j--;
		}
	}
	else {
		*(dir + j - 1) = '/';
		j--;
		while( *(dir + j - 1) != '/' ) {
		    j--;
		}
	}
	return (dir + j);
}
char *truncateRight(char *dir) {
	int j = stringLength(dir);
	if ( *(dir + j - 1) != '/') {
		while( *(dir + j - 1) != '/' ) {
			*(dir + j - 1) = '\0';
			j--;
		}
	}
	else {
		j--;
		while( *(dir + j - 1) != '/' ) {
		    	*(dir + j - 1) = '\0';
			j--;
		}
	}
	return dir;	
}
